<div id="tb<?php echo $uniqid;?>" style="padding:5px">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:;" onclick="eventcatAdd();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-tianjia1"></i><span class="l-btn-text">增加</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:;" onclick="eventcatEdit();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-bianji"></i><span class="l-btn-text">编辑</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="eventcatDelete();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shanchu"></i><span class="l-btn-text">删除</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:;" onclick="eventcatReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
            <td>&nbsp;&nbsp;<input id="eventcatKeywords" style="width:100px" type="text" class="easyui-input textbox"></td>
            <td><a href="javascript:void(0);" onclick="eventcatSearch();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span class="l-btn-text">查询</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="con" onselectstart="return false;" style="-moz-user-select:none;">
    <table id="eventcat"></table>
</div>
<div id="eventcatdlg" class="easyui-dialog" style="width:720px; height:560px;" title="信息框" closed="true"></div>
<script>
    var eventcatAdd = function () {
        $("#eventcatdlg").dialog({
            title: '添加事项主题',
            closed: false,
            width: 860,
            height: 560,
            cache: false,
            href: '/admin/eventcat/add',
            modal: true,
            buttons: [{
                text: '保存',
                id: 'eventcatsubmit',
                iconCls: 'icon-ok',
            }, {
                text: '关闭',
                iconCls: 'icon-no',
                handler: function () {
                    $("#eventcatdlg").dialog('close');
                }
            }]
        });
    };
    var eventcatEdit = function (id) {
        let row = $('#eventcat').datagrid('getSelected');
        let rowid = 0;
        if(!row){
            if(!id) {
                $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
            }else{
                rowid = id;
            }
        }else{
            rowid = row.id;
        }
        $("#eventcatdlg").dialog({
            title: '编辑信息',
            closed: false,
            width: 860,
            height: 560,
            cache: false,
            href: '/admin/eventcat/edit?id=' + rowid,
            modal: true,
            buttons: [{
                text: '保存',
                id: 'eventcatsubmit',
                iconCls: 'icon-ok',
            }, {
                text: '关闭',
                iconCls: 'icon-no',
                handler: function () {
                    $("#eventcatdlg").dialog('close');
                }
            }]
        });
    };
    var eventcatDelete = function () {
        var row = $('#eventcat').treegrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '确定删除吗？', function (r) {
                if (r == true) {
                    $.messager.progress();
                    $.post('/admin/eventcat/delete', {id: row.id}, function (data) {
                        $.messager.progress('close');
                        if (parseInt(data.ret) === 0) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            eventcatReload();
                        } else {
                            $.messager.alert('提示', data.msg, 'warning');
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.alert('提示', '请先选择要删除的行！', 'warning');
        }
    };
    var eventcatReload = function () {
        $("#eventcat").treegrid('reload');
    };
    var eventcatSearch = function () {
        var height = $(window).height() - $(".top").height() - 46;
        var width = $(window).width() - $(".leftmenu").width() - 13;
        $("#eventcat").treegrid({
            title: '事项主题',
            width: width,
            height: height,//高度
            singleSelect: true,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: true,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 1,
            pageList: [1, 5, 10, 25],
            method: 'post',
            sortName: 'sortorder',
            sortOrder: 'desc',
            idField: 'id',
            treeField: 'title',
            url: '/admin/eventcat/get',
            queryParams: {keywords: $('#eventcatkeywords').val()},
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: true,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: false,
            checkOnSelect: true,
            toolbar: '#tb<?php echo $uniqid;?>',
            columns: [[
                {field: 'title', title: '名称', width: fixWidth(20)},
                {field: 'sortorder', title: '排序', width: fixWidth(10)},
                {field: 'action', title: '操作', width: fixWidth(10), formatter: function (value, rowData, rowIndex) {
                        return '<a href="javascript:;" class="l-btn l-btn-small" onclick="eventcatEdit(' + rowData.id + ')" ><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">编辑</span><span class="l-btn-icon icon-ok">&nbsp;</span></span></a>  ' +
                            '<a href="javascript:;" class="l-btn l-btn-small" onclick="eventcatDelete(' + rowData.id + ')" ><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">删除</span><span class="l-btn-icon icon-cancel">&nbsp;</span></span></a>';
                    }
                },
            ]],
            onDblClickRow: function (index, field) {
                eventcatEdit();
            },
            // onClickRow: function (index, field) {
            //     var node = $('#eventcat').treegrid('getSelected');
            //     var child= $('#eventcat').treegrid("getChildren",node.id);
            //     if(child.length>0){
            //         $('#eventcat').treegrid('toggle', node.id);
            //     }else{
            //         $.ajax({
            //             type: 'POST',
            //             url: '/admin/eventcat/getchildren/?id=' + node.id,
            //             data: {
            //                 id: node.id,
            //                 keywords: $('#keywords').val()
            //             },
            //             dataType: 'json',
            //             timeout: 30000,
            //             async: true,
            //             cache: false,
            //             context: $('body'),
            //             success: function (ret) {
            //                 if(ret.length > 0){
            //                     $('#eventcat').treegrid('append', {
            //                         parent: node.id,
            //                         data: ret,
            //                     });
            //                 }
            //             },
            //         });
            //     }
            // },
            onLoadSuccess: function (row, data) {
                // $.each(data, function (i, val) {
                //     $('#eventcat').treegrid('collapseAll', data[i].id)
                // })
            }
        });
    };
    eventcatSearch();
</script>
